package br.com.toscano.contpedido.view;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import br.com.toscano.contpedido.business.PedidoBC;
import br.com.toscano.contpedido.otd.UsuarioVendedorOTD;
import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;


@ViewController
@NextView("./usuario_vendedor_list.jsf")
@PreviousView("./usuario_vendedor_list.jsf")
public class UsuarioVendedorListMB extends AbstractListPageBean<UsuarioVendedorOTD, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private PedidoBC pedidoBC;
	
	private Date dataInicial;
	private Date dataFinal;


	public Date getDataInicial() {
		return dataInicial;
	}

	public void setDataInicial(Date dataInicial) {
		this.dataInicial = dataInicial;
	}

	public Date getDataFinal() {
		return dataFinal;
	}

	public void setDataFinal(Date dataFinal) {
		this.dataFinal = dataFinal;
	}

	private boolean filtrar = false;

	public void filtrar() {
		filtrar = true;
		super.clear();
		super.getResultList();
	}
	
	@Override
	protected List<UsuarioVendedorOTD> handleResultList() {
		List<UsuarioVendedorOTD> retorno = null;
		if (filtrar) {
			retorno = pedidoBC.gerarUsuarioVendedor(dataInicial, dataFinal);
		}

		return retorno;
	}

	@Transactional
	public String deleteSelection() {
		boolean delete;
		for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
			Long id = iter.next();
			delete = getSelection().get(id);
			if (delete) {
				pedidoBC.registrarCancelamento(id);
				iter.remove();
			}
		}
		return getPreviousView();
	}
	
	public void carregarPedido() {
		
	}
}